//
//  JYTRegisteredHandler.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/11/30.
//  Copyright © 2016年 com.yuan. All rights reserved.
//

#import "JYTRegisteredHandler.h"
#import "User.h"
#import "JYTUserManager.h"

@implementation JYTRegisteredHandler

+ (JYTRegisteredHandler *) shared
{
    static dispatch_once_t onceToken;
    static JYTRegisteredHandler *checkHandler;
    dispatch_once(&onceToken, ^{
        checkHandler = [[JYTRegisteredHandler alloc] init];
    });
    return checkHandler;
}

//获取验证码
- (void) getPinCodeWithPhone:(NSString *)phoneNum withHandler:(void (^) (NSString *userName)) handler
{
    NSString *phone =  phoneNum;
    if (!phone || [phone isEqualToString:@" "] || [phone isEqualToString:@""]) {
        return;
    }
    
    JYTHttpClient *client = [JYTHttpClient sharedInstance];
    
    client.urlType = userCenterUrl;
    
    client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg){
        
        if (!result) {
            handler(nil);
        }
        handler(code);
    };
    
    client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
        
        handler(code);
    };
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //请求 参数
    [param setObject:phoneNum forKey:@"UserName"];
    
    //请求 CommandCode
    [param setObject:@"SendSecurityCode" forKey:@"CommandCode"];
    
    [client post:@"GetDataInfo" withParam:param];
}


-(void)checkPinCodeWithPhone:(NSString *)phoneNum pinCode:(NSString *)pinCode withHandler:(void (^)(NSString *message, NSString *code))handler
{
    JYTHttpClient *client = [JYTHttpClient sharedInstance];
    
    client.urlType = userCenterUrl;
    
    
    client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg){
        
         handler(msg, code);
  
    };
    
    client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
        
        handler(msg, code);
        
    };
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //请求 参数
    [param setObject:phoneNum forKey:@"UserName"];
    [param setObject:pinCode forKey:@"Code"];
    
    //请求 CommandCode
    [param setObject:@"SecurityCode" forKey:@"CommandCode"];
    
    [client post:@"GetDataInfo" withParam:param];
}


- (void) getCenterPinCodeWithPhone:(NSString *)phoneNum withHandler:(void (^) (NSString *code, NSString *userName)) handler
{
    NSString *phone =  phoneNum;
    if (!phone || [phone isEqualToString:@" "] || [phone isEqualToString:@""]) {
        return;
    }
    
    JYTHttpClient *client = [JYTHttpClient sharedInstance];
    
    client.urlType = userCenterUrl;
    
    client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg){
        
        if (!result) {
            handler(nil, nil);
        }
        
        NSDictionary *data = result[@"data"];
        NSString *userName = data[@"UserName"];

        handler(code,userName);
    };
    
    client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
        
        handler(code, nil);
    };
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //请求 参数
    [param setObject:phoneNum forKey:@"TelPhone"];
    
    //请求 CommandCode
    [param setObject:@"VerificationCode" forKey:@"CommandCode"];
    
    [client post:@"GetDataInfo" withParam:param];
}


- (void) registerWithParams:(NSDictionary *)params completionHandler:(void (^)(BOOL succ)) handler
{
    if (params) {
        
        JYTHttpClient *client = [JYTHttpClient sharedInstance];
        
        client.urlType = userCenterUrl;
        
        client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg){
            MyLog(@"##############%@", result);
            NSDictionary *dic = result;
            dic = dic[@"data"];
            
            User *usr = [User mj_objectWithKeyValues:dic];
            
            //保存登录名称
            [[JYTUserManager shared] setUserLoginName:usr.userName];
            
            if (usr) {
                [[JYTUserManager shared] setUser:usr];
            }
            
            //设置设置登录状态
            [[JYTUserManager shared] setUsedOffLine:@"0"];
            
            MyLog(@"\n\n%@", dic);
            
            handler(YES);
        };
        
        client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
            //设置设置登录状态
            [[JYTUserManager shared] setUsedOffLine:@"1"];
            
            handler(NO);
        };
        
        
 
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        
        //请求 参数
        [param setDictionary:params];
        
        //请求 CommandCode
        [param setObject:@"Register" forKey:@"CommandCode"];
        
        [client post:@"AddDataInfo" withParam:param];
        
    } else {
        [[JYTUserManager shared] removeSaveInfomation];
        
        handler(NO);
    }
}


@end
